package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public Map getItem(Long skuId) {
        Map map = new HashMap();
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        BaseCategoryView categoryView = productFeignClient.getGategoryView(skuInfo.getCategory3Id());
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        List<SpuPoster> spuPosterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
        //  根据skuId 获取平台属性数据
        List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
        List<HashMap<String, String>> mapList = attrList.stream().map(new Function<BaseAttrInfo, HashMap<String, String>>() {
            @Override
            public HashMap<String, String> apply(BaseAttrInfo baseAttrInfo) {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }
        }).collect(Collectors.toList());
        //  获取销售属性
        List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
        //  获取切换功能数据.
        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        String jsonStr = JSON.toJSONString(skuValueIdsMap);
        //  存储数据
        map.put("skuInfo",skuInfo);
        map.put("categoryView",categoryView);
        map.put("price",skuPrice);
        map.put("spuPosterList",spuPosterList);
        map.put("skuAttrList",mapList);
        map.put("spuSaleAttrList",spuSaleAttrList);
        map.put("valuesSkuJson",jsonStr);
        return map;
    }
}
